/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.dataprovider.control.logic.fasta;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class FastaSourceConverter {

    private static final Logger LOGGER = Logger.getLogger(FastaSourceConverter.class);

    /**
     *
     * @param input
     * @return the bytearray inputstream for the given object (returns null if the conversion is impossible)
     * @throws IOException
     * @throws NullPointerException
     */
    public static ByteArrayInputStream convertToStream(Object input) throws IOException,NullPointerException {
        ByteArrayInputStream inputStream = null;
            if (input == null) {
                throw new IOException("Cannot create inputstream from nothing");
            }
            if (input instanceof String) {
                inputStream = new ByteArrayInputStream(((String) input).getBytes("UTF-8"));
            } else if (input instanceof StringBuilder) {
                inputStream = new ByteArrayInputStream(input.toString().getBytes("UTF-8"));
            } else if (input instanceof URL) {
                inputStream = getStreamFromURL((URL) input);
            } else if (input instanceof File) {
                inputStream = new ByteArrayInputStream(FileUtils.readFileToByteArray((File) input));
            } else if (input instanceof ByteArrayInputStream) {
                inputStream = (ByteArrayInputStream) input;
            } else if (input instanceof ByteArrayOutputStream) {
                inputStream = redirectOutputStream((ByteArrayOutputStream) input);
            } else if (input instanceof HttpURLConnection) {
                inputStream = convertInputStream(((HttpURLConnection) input).getInputStream());
            } else if (input instanceof InputStream) {
                inputStream = convertInputStream((InputStream) inputStream);
            }
        return inputStream;
    }

    private static ByteArrayInputStream convertInputStream(InputStream input) throws IOException {
        byte[] buff = new byte[8000];
        int bytesRead;
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        while ((bytesRead = input.read(buff)) != -1) {
            bao.write(buff, 0, bytesRead);
        }
        byte[] data = bao.toByteArray();
        return new ByteArrayInputStream(data);
    }

    private static ByteArrayInputStream getStreamFromURL(URL input) throws IOException {
        ByteArrayOutputStream bais = new ByteArrayOutputStream();
        // localhost:52999 is a reverse proxy to xxx:443
        java.net.URLConnection conn = input.openConnection();
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
        conn.connect();
        try (InputStream is = input.openStream()) {
            byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
            int n;
            while ((n = is.read(byteChunk)) > 0) {
                bais.write(byteChunk, 0, n);
            }
            return redirectOutputStream(bais);
        }
    }

    /**
     *
     * @param outputStream
     * @return the redirected outputstream as a bytearray inputstream
     */
    public static ByteArrayInputStream redirectOutputStream(ByteArrayOutputStream outputStream) {
        return new ByteArrayInputStream(outputStream.toByteArray());
    }

}
